package com.stone.sys.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import com.stone.sys.pojo.TBzgsk;
import com.stone.sys.service.SysCommonService;
import com.stone.utils.JsonUtils;
import com.stone.utils.StringUtils;

import frm.ssh.action.BaseListAction;
import frm.ssh.exception.AppException;

public class BzgskAction extends BaseListAction {
	
	public HashMap<String, Object> setSearchCondition() {
		return null;
	}

	private static final long serialVersionUID = 865527685459140622L;

	private SysCommonService myService;
	
	private TBzgsk entity;
	
	private String id;
	
	public void getBzgskList() throws IOException, AppException {
		
		//
		String conditions = " where 1 = 1";
		String sql = 	"select a.id as id,(select organ_name from sys_organ b where b.id = a.bzid) as bzname," +
						" (select item_name from sys_dictionary c where c.item_id = a.dydj and c.item_type = 'DYDJ') as dydj," +
						" (select item_name from sys_dictionary d where d.item_id = a.sblx and d.item_type = 'SBLX') as sblx," +
						" a.zymc as zymc,a.bzgs as bzgs" +
						" from tab_bzgsk a";
			
		if(!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon1())) {
			conditions += " and a.bzid = '" + super.getSearchBean().getCon1() + "'";
		}
		
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon2())) {
			conditions += " AND a.dydj = '" + super.getSearchBean().getCon2() + "'";
		}
		
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon3())) {
			conditions += " AND a.sblx = '" + super.getSearchBean().getCon3() + "'";
		}
		
		sql += conditions;
		
		List list = myService.findListBySql(sql);
		
        JsonConfig jsonConfig = JsonUtils.configJson(new String[] {}, "yyyy-MM-dd");
    	
        JSON json = JSONSerializer.toJSON(list, jsonConfig);

		String strjson = "{result:" + json.toString() + ",totalCount:[" + list.size() + "]}";
		
        super.getResponse().getWriter().print(strjson);
		
	}
	
	public void save() throws IOException, AppException {
		
		//long count = 0;
		
		//String out = "{rep:true,success:true,msg:'编号重复',id:'" + entity.getId() + "'}";
		
		//新建
		if (entity.getId() == 0) {
			myService.save(entity);
			super.getResponse().getWriter().print("{success:true,msg:'保存成功',id:'" + entity.getId() + "'}");
		}
	}
			

	
	public void delete() throws Exception {
		
		myService.doDelAll(id, TBzgsk.class.getName());
		
		super.getResponse().getWriter().print("删除成功");
		
	}
	
	public void getBz() throws Exception{
		
		List templist = myService.findListByHql("from TOrgan");
		
		JsonConfig jsonConfig = JsonUtils.configJson(new String[] {},
		"yyyy-MM-dd");

		JSON json = JSONSerializer.toJSON(templist, jsonConfig);
		
		super.getResponse().getWriter().print(json.toString());
	}

	public SysCommonService getMyService() {
		return myService;
	}

	public void setMyService(SysCommonService myService) {
		this.myService = myService;
	}

	public TBzgsk getEntity() {
		return entity;
	}

	public void setEntity(TBzgsk entity) {
		this.entity = entity;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

}
